﻿using CallCenter.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenter.Biz
{
    /// <summary>
    /// 外呼接口
    /// </summary>
    public class OutgoingCall
    {
        /// <summary>
        /// 外呼
        /// </summary>
        /// <param name="phoneNumber">外呼的电话号码</param>
        /// <returns>Task ID</returns>
        public string Call(string url, string enterpriceId, string phoneNumber)
        {
            // http://{{ServerIP}}:{{chatport}}/autodailtask.ashx
            // http://192.168.1.162:88/autodailtask.ashx?enterid=1&logname=AF26231672&dialtype=6&phone=83235180&voc=welcom.wav&datatype=json

            string requestUrl = "http://" + url + "/autodailtask.ashx?";
            requestUrl += "enterid=" + enterpriceId;
            requestUrl += "&logname=" + "AF90114782";
            requestUrl += "&dialtype=0";                // 人工接听
            requestUrl += "&phone=" + phoneNumber;
            requestUrl += "&lognamelist=" + "700";
            requestUrl += "&datatype=json";

            string response = HttpHelper.HttpGet(requestUrl);
            if(string.IsNullOrEmpty(response))
            {
                return "";
            }

            // [{"phonenumber":"83235180","taskdetailid":"123"}]
            Dictionary<string, string>[] results = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(response);
            if(results == null || results.Length == 0 || !results[0].ContainsKey("taskdetailid"))
            {
                return "";
            }

            return results[0]["taskdetailid"];
        }
    }
}
